package hn.cch.mybatis.base;

import org.mybatis.spring.mapper.MapperFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;

import java.io.IOException;

/**
 * 后置处理器：处理@Mapper注解，接口上注解
 */
public class MapperPostProcessor implements BeanDefinitionRegistryPostProcessor {

    private static final Logger logger = LoggerFactory.getLogger(MapperPostProcessor.class);

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        try {
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources("classpath*:hn/cch/mybatis/base/mapper/**/*.class");
            AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
            CachingMetadataReaderFactory readerFactory = new CachingMetadataReaderFactory();
            for (Resource resource : resources) {
                MetadataReader metadataReader = readerFactory.getMetadataReader(resource);
                ClassMetadata classMetadata = metadataReader.getClassMetadata();
                if (classMetadata.isInterface()) { // 只处理接口
                    AbstractBeanDefinition abstractBeanDefinition = BeanDefinitionBuilder
                        .genericBeanDefinition(MapperFactoryBean.class)// 接口实现类
                        .addConstructorArgValue(classMetadata.getClassName())
                        .setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE)
                        .getBeanDefinition();
                    AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.
                        genericBeanDefinition(classMetadata.getClassName()).getBeanDefinition();
                    String beanName = beanNameGenerator.generateBeanName(beanDefinition, beanDefinitionRegistry);
                    beanDefinitionRegistry.registerBeanDefinition(beanName, abstractBeanDefinition);
                }
            }
        } catch (IOException e) {
            logger.error("", e);
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
